home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Dev / Oberon / source / amiga / IFFParse.mod < prev    next >
Text File  |  1995-06-29  |  15KB  |  522 lines

  1. (**************************************************************************
  2.  
  3.      $RCSfile: IFFParse.mod $
  4.   Description: Interface to iffparse.library
  5.  
  6.    Created by: fjc (Frank Copeland)
  7.     $Revision: 3.8 $
  8.       $Author: fjc $
  9.         $Date: 1995/06/04 23:13:14 $
  10.  
  11.   Includes Release 40.15
  12.  
  13.   (C) Copyright 1985-1993 Commodore-Amiga, Inc.
  14.       All Rights Reserved
  15.  
  16.   Oberon-A interface Copyright © 1994-1995, Frank Copeland.
  17.   This file is part of the Oberon-A Interface.
  18.   See Oberon-A.doc for conditions of use and distribution.
  19.  
  20. ***************************************************************************)
  21.  
  22. <* STANDARD- *>
  23.  
  24. MODULE [2] IFFParse;
  25.  
  26. IMPORT
  27.   SYS := SYSTEM, Kernel, e := Exec, c := Clipboard, u := Utility,
  28.   s := Sets;
  29.  
  30. (*
  31. **      $VER: iffparse.h 39.1 (1.6.92)
  32. **
  33. **      iffparse.library structures and constants
  34. *)
  35.  
  36.  
  37. TYPE
  38.  
  39. (* Structure associated with an active IFF stream.
  40.  * "stream" is a value used by the client's read/write/seek functions -
  41.  * it will not be accessed by the library itself and can have any value
  42.  * (could even be a pointer or a BPTR).
  43.  *
  44.  * This structure can only be allocated by iffparse.library
  45.  *)
  46.   IFFHandlePtr * = POINTER TO IFFHandle;
  47.   IFFHandle * = RECORD
  48.     stream * : e.ULONG;
  49.     flags *  : s.SET32;
  50.     depth *  : LONGINT;      (*  Depth of context stack.  *)
  51.   END; (* IFFHandle *)
  52.  
  53. (*
  54.  * Bit masks for "IFFHandle.flags" field.
  55.  *)
  56.  
  57. CONST
  58.  
  59.   read *       = {};                     (* read mode - default *)
  60.   write *      = {0};                    (* write mode *)
  61.   rwBits *     = read + write;           (* read/write bits *)
  62.   fSeek *      = {1};                    (* forward seek only *)
  63.   rSeek *      = {2};                    (* random seek *)
  64.   reserved *   = {16 .. 31};             (* Don't touch these bits. *)
  65.  
  66. (*****************************************************************************)
  67.  
  68. (*
  69.  * When the library calls your stream handler, you'll be passed a pointer
  70.  * to this structure as the "message packet".
  71.  *)
  72.  
  73. TYPE
  74.  
  75.   IFFStreamCmdPtr * = POINTER TO IFFStreamCmd;
  76.   IFFStreamCmd * = RECORD
  77.     command * : LONGINT;     (*  Operation to be performed (cmd...) *)
  78.     buf *     : e.APTR;      (*  Pointer to data buffer              *)
  79.     nBytes *  : LONGINT;     (*  Number of bytes to be affected      *)
  80.   END; (* IFFStreamCmd *)
  81.  
  82. (*****************************************************************************)
  83.  
  84. (*
  85.  * A node associated with a context on the iffStack.  Each node
  86.  * represents a chunk, the stack representing the current nesting
  87.  * of chunks in the open IFF file.  Each context node has associated
  88.  * local context items in the (private) LocalItems list.  The ID, type,
  89.  * size and scan values describe the chunk associated with this node.
  90.  *
  91.  * This structure can only be allocated by iffparse.library
  92.  *)
  93.  
  94. TYPE
  95.  
  96.   ContextNodePtr * = POINTER TO ContextNode;
  97.   ContextNode * = RECORD (e.MinNodeBase)
  98.     node * : e.MinNode;
  99.     id *   : LONGINT;
  100.     type * : LONGINT;
  101.     size * : LONGINT;        (*  Size of this chunk             *)
  102.     scan * : LONGINT;        (*  # of bytes read/written so far *)
  103.   END; (* ContextNode *)
  104.  
  105. (*****************************************************************************)
  106.  
  107. (*
  108.  * Local context items live in the ContextNode's.  Each class is identified
  109.  * by its lciIdent code and has a (private) purge vector for when the
  110.  * parent context node is popped.
  111.  *
  112.  * This structure can only be allocated by iffparse.library
  113.  *)
  114.  
  115. TYPE
  116.  
  117.   LocalContextItemPtr * = POINTER TO LocalContextItem;
  118.   LocalContextItem * = RECORD (e.MinNodeBase)
  119.     node *  : e.MinNode;
  120.     id *    : e.ULONG;
  121.     type *  : e.ULONG;
  122.     ident * : e.ULONG;
  123.   END; (* LocalContextItem *)
  124.  
  125. (*****************************************************************************)
  126.  
  127. (*
  128.  * StoredProperty: a local context item containing the data stored
  129.  * from a previously encountered property chunk.
  130.  *)
  131.  
  132. TYPE
  133.  
  134.   StoredPropertyPtr * = POINTER TO StoredProperty;
  135.   StoredProperty * = RECORD
  136.     size * : LONGINT;
  137.     data * : e.APTR;
  138.   END; (* StoredProperty *)
  139.  
  140. (*****************************************************************************)
  141.  
  142. (*
  143.  * Collection Item: the actual node in the collection list at which
  144.  * client will look.  The next pointers cross context boundaries so
  145.  * that the complete list is accessable.
  146.  *)
  147.  
  148. TYPE
  149.  
  150.   CollectionItemPtr * = POINTER TO CollectionItem;
  151.   CollectionItem * = RECORD
  152.     next * : CollectionItemPtr;
  153.     size * : LONGINT;
  154.     data * : e.APTR;
  155.   END; (* CollectionItem *)
  156.  
  157. (*****************************************************************************)
  158.  
  159. (*
  160.  * Structure returned by OpenClipboard().  You may do cmdPosts and such
  161.  * using this structure.  However, once you call OpenIFF(), you may not
  162.  * do any more of your own I/O to the clipboard until you call CloseIFF().
  163.  *)
  164.  
  165. TYPE
  166.  
  167.   ClipboardHandlePtr * = POINTER TO ClipboardHandle;
  168.   ClipboardHandle * = RECORD (c.IOClipReqBase)
  169.     req *         : c.IOClipReq;
  170.     cbport *      : e.MsgPort;
  171.     satisfyPort * : e.MsgPort;
  172.   END; (* ClipboardHandle *)
  173.  
  174. (*****************************************************************************)
  175.  
  176. (*
  177.  * IFF return codes.  Most functions return either zero for success or
  178.  * one of these codes.  The exceptions are the read/write functions which
  179.  * return positive values for number of bytes or records read or written,
  180.  * or a negative error code.  Some of these codes are not errors per sae,
  181.  * but valid conditions such as EOF or EOC (End of Chunk).
  182.  *)
  183.  
  184. CONST
  185.  
  186.   errEOF *              = -1;     (*  Reached logical end of file *)
  187.   errEOC *              = -2;     (*  About to leave context      *)
  188.   errNoScope *          = -3;     (*  No valid scope for property *)
  189.   errNoMem *            = -4;     (*  Internal memory alloc failed*)
  190.   errRead *             = -5;     (*  Stream read error           *)
  191.   errWrite *            = -6;     (*  Stream write error          *)
  192.   errSeek *             = -7;     (*  Stream seek error           *)
  193.   errMangled *          = -8;     (*  Data in file is corrupt     *)
  194.   errSyntax *           = -9;     (*  IFF syntax error            *)
  195.   errNotIFF *           = -10;    (*  Not an IFF file             *)
  196.   errNoHook *           = -11;    (*  No call-back hook provided  *)
  197.   return2Client *       = -12;    (*  Client handler normal return*)
  198.  
  199. (*****************************************************************************)
  200.  
  201. (*
  202.  * Universal IFF identifiers.
  203.  *)
  204.  
  205. CONST
  206.  
  207.   idFORM *   = 0464F524DH;  (* MakeID('F','O','R','M') *)
  208.   idLIST *   = 04C495354H;  (* MakeID('L','I','S','T') *)
  209.   idCAT *    = 043415420H;  (* MakeID('C','A','T',' ') *)
  210.   idPROP *   = 050524F50H;  (* MakeID('P','R','O','P') *)
  211.   idNULL *   = 020202020H;  (* MakeID(' ',' ',' ',' ') *)
  212.  
  213. (*
  214.  * Identifier codes for universally recognized local context items.
  215.  *)
  216.  
  217. CONST
  218.  
  219.   lciPROP *         = 070726F70H;  (* MakeID('p','r','o','p') *)
  220.   lciCOLLECTION *   = 0636F6C6CH;  (* MakeID('c','o','l','l') *)
  221.   lciENTRYHANDLER * = 0656E6864H;  (* MakeID('e','n','h','d') *)
  222.   lciEXITHANDLER *  = 065786864H;  (* MakeID('e','x','h','d') *)
  223.  
  224. (*****************************************************************************)
  225.  
  226. (*
  227.  * Control modes for ParseIFF() function.
  228.  *)
  229.  
  230. CONST
  231.  
  232.   parseScan *           = 0;
  233.   parseStep *           = 1;
  234.   parseRawStep *        = 2;
  235.  
  236. (*****************************************************************************)
  237.  
  238. (*
  239.  * Control modes for StoreLocalItem().
  240.  *)
  241.  
  242. CONST
  243.  
  244.   sliRoot *             = 1;      (*  Store in default context       *)
  245.   sliTop *              = 2;      (*  Store in current context       *)
  246.   sliProp *             = 3;      (*  Store in topmost FORM or LIST  *)
  247.  
  248. (*****************************************************************************)
  249.  
  250. (* Magic value for writing functions. If you pass this value in as a size
  251.  * to PushChunk() when writing a file, the parser will figure out the
  252.  * size of the chunk for you. If you know the size, is it better to
  253.  * provide as it makes things faster.
  254.  *)
  255.  
  256.   sizeUnknown *         = -1;
  257.  
  258. (*****************************************************************************)
  259.  
  260. (*
  261.  * Possible call-back command values.  (Using 0 as the value for iffCmdInit
  262.  * was, in retrospect, probably a bad idea.)
  263.  *)
  264.  
  265. CONST
  266.  
  267.   cmdInit *     = 0;       (*  Prepare the stream for a session    *)
  268.   cmdCleanup *  = 1;       (*  Terminate stream session            *)
  269.   cmdRead *     = 2;       (*  Read bytes from stream              *)
  270.   cmdWrite *    = 3;       (*  Write bytes to stream               *)
  271.   cmdSeek *     = 4;       (*  Seek on stream                      *)
  272.   cmdEntry *    = 5;       (*  You just entered a new context      *)
  273.   cmdExit *     = 6;       (*  You're about to leave a context     *)
  274.   cmdPurgeLCI * = 7;       (*  Purge a LocalContextItem            *)
  275.  
  276. (*****************************************************************************)
  277.  
  278. (* Obsolete IFFParse definitions, here for source code compatibility only.
  279.  * Please do NOT use in new code.
  280.  *)
  281. CONST
  282.  
  283.   sccInit *     = cmdInit;
  284.   sccCleanup *  = cmdCleanup;
  285.   sccRead *     = cmdRead;
  286.   sccWrite *    = cmdWrite;
  287.   sccSeek *     = cmdSeek;
  288.  
  289.  
  290. (*-- Library Base variable --------------------------------------------*)
  291.  
  292. CONST
  293.  
  294.   iffparseName * = "iffparse.library";
  295.  
  296. VAR
  297.  
  298.   base * : e.LibraryPtr;
  299.  
  300.  
  301. (*-- Library Functions ------------------------------------------------*)
  302.  
  303. (*
  304. **      $VER: iffparse_protos.h 39.1 (1.6.92)
  305. *)
  306.  
  307. (*--- functions in V36 or higher (Release 2.0) ---*)
  308.  
  309. (* ------ Basic functions ------*)
  310.  
  311. PROCEDURE AllocIFF* [base,-30] ()
  312.   : IFFHandlePtr;
  313. PROCEDURE OpenIFF* [base,-36]
  314.   ( iff    [8] : IFFHandlePtr;
  315.     rwMode [0] : s.SET32 )
  316.   : LONGINT;
  317. PROCEDURE ParseIFF* [base,-42]
  318.   ( iff     [8] : IFFHandlePtr;
  319.     control [0] : LONGINT )
  320.   : LONGINT;
  321. PROCEDURE CloseIFF* [base,-48]
  322.   ( iff [8] : IFFHandlePtr );
  323. PROCEDURE FreeIFF* [base,-54]
  324.   ( iff [8] : IFFHandlePtr );
  325.  
  326. (* ------ Read/Write functions ------*)
  327.  
  328. PROCEDURE ReadChunkBytes* [base,-60]
  329.   ( iff      [8] : IFFHandlePtr;
  330.     VAR buf  [9] : ARRAY OF SYS.BYTE;
  331.     size     [0] : LONGINT )
  332.   : LONGINT;
  333. PROCEDURE WriteChunkBytes* [base,-66]
  334.   ( iff  [8] : IFFHandlePtr;
  335.     buf  [9] : ARRAY OF SYS.BYTE;
  336.     size [0] : LONGINT )
  337.   : LONGINT;
  338. PROCEDURE ReadChunkRecords* [base,-72]
  339.   ( iff            [8] : IFFHandlePtr;
  340.     VAR buf        [9] : ARRAY OF SYS.BYTE;
  341.     bytesPerRecord [0] : LONGINT;
  342.     nRecords       [1] : LONGINT )
  343.   : LONGINT;
  344. PROCEDURE WriteChunkRecords* [base,-78]
  345.   ( iff            [8] : IFFHandlePtr;
  346.     buf            [9] : ARRAY OF SYS.BYTE;
  347.     bytesPerRecord [0] : LONGINT;
  348.     nRecords       [1] : LONGINT )
  349.   : LONGINT;
  350.  
  351. (* ------ Context entry/exit ------*)
  352.  
  353. PROCEDURE PushChunk* [base,-84]
  354.   ( iff  [8] : IFFHandlePtr;
  355.     type [0] : LONGINT;
  356.     id   [1] : LONGINT;
  357.     size [2] : LONGINT )
  358.   : LONGINT;
  359. PROCEDURE PopChunk* [base,-90]
  360.   ( iff [8] : IFFHandlePtr )
  361.   : LONGINT;
  362.  
  363. (* ------ Low-level handler installation ------*)
  364.  
  365. PROCEDURE EntryHandler* [base,-102]
  366.   ( iff      [8] : IFFHandlePtr;
  367.     type     [0] : LONGINT;
  368.     id       [1] : LONGINT;
  369.     position [2] : LONGINT;
  370.     handler  [9] : u.HookPtr;
  371.     object  [10] : e.APTR )
  372.   : LONGINT;
  373. PROCEDURE ExitHandler* [base,-108]
  374.   ( iff      [8] : IFFHandlePtr;
  375.     type     [0] : LONGINT;
  376.     id       [1] : LONGINT;
  377.     position [2] : LONGINT;
  378.     handler  [9] : u.HookPtr;
  379.     object  [10] : e.APTR )
  380.   : LONGINT;
  381.  
  382. (* ------ Built-in chunk/property handlers ------*)
  383.  
  384. PROCEDURE PropChunk* [base,-114]
  385.   ( iff  [8] : IFFHandlePtr;
  386.     type [0] : LONGINT;
  387.     id   [1] : LONGINT )
  388.   : LONGINT;
  389. PROCEDURE PropChunks* [base,-120]
  390.   ( iff       [8] : IFFHandlePtr;
  391.     propArray [9] : ARRAY OF LONGINT;
  392.     nProps    [0] : LONGINT )
  393.   : LONGINT;
  394. PROCEDURE StopChunk* [base,-126]
  395.   ( iff  [8] : IFFHandlePtr;
  396.     type [0] : LONGINT;
  397.     id   [1] : LONGINT )
  398.   : LONGINT;
  399. PROCEDURE StopChunks* [base,-132]
  400.   ( iff       [8] : IFFHandlePtr;
  401.     propArray [9] : ARRAY OF LONGINT;
  402.     nProps    [0] : LONGINT )
  403.   : LONGINT;
  404. PROCEDURE CollectionChunk* [base,-138]
  405.   ( iff  [8] : IFFHandlePtr;
  406.     type [0] : LONGINT;
  407.     id   [1] : LONGINT )
  408.   : LONGINT;
  409. PROCEDURE CollectionChunks* [base,-144]
  410.   ( iff       [8] : IFFHandlePtr;
  411.     propArray [9] : ARRAY OF LONGINT;
  412.     nProps    [0] : LONGINT )
  413.   : LONGINT;
  414. PROCEDURE StopOnExit* [base,-150]
  415.   ( iff  [8] : IFFHandlePtr;
  416.     type [0] : LONGINT;
  417.     id   [1] : LONGINT )
  418.   : LONGINT;
  419.  
  420. (* ------ Context utilities ------*)
  421.  
  422. PROCEDURE FindProp* [base,-156]
  423.   ( iff  [8] : IFFHandlePtr;
  424.     type [0] : LONGINT;
  425.     id   [1] : LONGINT )
  426.   : StoredPropertyPtr;
  427. PROCEDURE FindCollection* [base,-162]
  428.   ( iff  [8] : IFFHandlePtr;
  429.     type [0] : LONGINT;
  430.     id   [1] : LONGINT )
  431.   : CollectionItemPtr;
  432. PROCEDURE FindPropContext* [base,-168]
  433.   ( iff [8] : IFFHandlePtr )
  434.   : ContextNodePtr;
  435. PROCEDURE CurrentChunk* [base,-174]
  436.   ( iff [8] : IFFHandlePtr )
  437.   : ContextNodePtr;
  438. PROCEDURE ParentChunk* [base,-180]
  439.   ( contextNode [8] : ContextNodePtr )
  440.   : ContextNodePtr;
  441.  
  442. (* ------ LocalContextItem support functions ------*)
  443.  
  444. PROCEDURE AllocLocalItem* [base,-186]
  445.   ( type     [0] : LONGINT;
  446.     id       [1] : LONGINT;
  447.     ident    [2] : LONGINT;
  448.     dataSize [3] : LONGINT )
  449.   : LocalContextItemPtr;
  450. PROCEDURE LocalItemData* [base,-192]
  451.   ( localItem [8] : LocalContextItemPtr )
  452.   : e.APTR;
  453. PROCEDURE SetLocalItemPurge* [base,-198]
  454.   ( localItem [8] : LocalContextItemPtr;
  455.     purgeHook [9] : u.HookPtr );
  456. PROCEDURE FreeLocalItem* [base,-204]
  457.   ( localItem [8] : LocalContextItemPtr );
  458. PROCEDURE FindLocalItem* [base,-210]
  459.   ( iff   [8] : IFFHandlePtr;
  460.     type  [0] : LONGINT;
  461.     id    [1] : LONGINT;
  462.     ident [2] : LONGINT )
  463.   : LocalContextItemPtr;
  464. PROCEDURE StoreLocalItem* [base,-216]
  465.   ( iff       [8] : IFFHandlePtr;
  466.     localItem [9] : LocalContextItemPtr;
  467.     position  [0] : LONGINT )
  468.   : LONGINT;
  469. PROCEDURE StoreItemInContext* [base,-222]
  470.   ( iff          [8] : IFFHandlePtr;
  471.     localItem    [9] : LocalContextItemPtr;
  472.     contextNode [10] : ContextNodePtr );
  473.  
  474. (* ------ IFFHandle initialization ------*)
  475.  
  476. PROCEDURE InitIFF* [base,-228]
  477.   ( iff        [8] : IFFHandlePtr;
  478.     flags      [0] : s.SET32;
  479.     streamHook [9] : u.HookPtr );
  480. PROCEDURE InitIFFasDOS* [base,-234]
  481.   ( iff [8] : IFFHandlePtr );
  482. PROCEDURE InitIFFasClip* [base,-240]
  483.   ( iff [8] : IFFHandlePtr );
  484.  
  485. (* ------ Internal clipboard support ------*)
  486.  
  487. PROCEDURE OpenClipboard* [base,-246]
  488.   ( unitNum [0] : LONGINT )
  489.   : ClipboardHandlePtr;
  490. PROCEDURE CloseClipboard* [base,-252]
  491.   ( clipboard [8] : ClipboardHandlePtr );
  492.  
  493. (* ------ Miscellaneous ------*)
  494.  
  495. PROCEDURE GoodID* [base,-258]
  496.   ( id [0] : LONGINT )
  497.   : LONGINT;
  498. PROCEDURE GoodType* [base,-264]
  499.   ( type [0] : LONGINT )
  500.   : LONGINT;
  501. PROCEDURE IDtoStr* [base,-270]
  502.   ( id      [0] : LONGINT;
  503.     VAR buf [8] : ARRAY OF CHAR )
  504.   : e.LSTRPTR;
  505.  
  506.  
  507. (*-- Library Base variable --------------------------------------------*)
  508.  
  509. <*$LongVars-*>
  510.  
  511. (*-----------------------------------*)
  512. PROCEDURE* [0] CloseLib (VAR rc : LONGINT);
  513.  
  514. BEGIN (* CloseLib *)
  515.   IF base # NIL THEN e.CloseLibrary (base) END
  516. END CloseLib;
  517.  
  518. BEGIN
  519.   base := e.OpenLibrary (iffparseName, e.libraryMinimum);
  520.   IF base # NIL THEN Kernel.SetCleanup (CloseLib) END
  521. END IFFParse.
  522.